package com.wjm.client.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.wjm.annotation.LoginValidate;
import com.wjm.entity.Prestige;
import com.wjm.entity.StuRecInfo;
import com.wjm.entity.Student;
import com.wjm.service.impl.PrestigeServiceImp;
import com.wjm.service.impl.RecordServiceImpl;
import com.wjm.service.impl.StudentServiceImpl;
import com.wjm.util.CacheUtil;
import com.wjm.util.StringUtil;

@Controller
@RequestMapping("/client/stu")
@SessionAttributes({ "student" })
public class StudentClientController {

	@Resource(name = "studentService")
	private StudentServiceImpl studentService;

	@Resource(name = "prestigeService")
	private PrestigeServiceImp prestigeService;

	@Resource(name = "recordService")
	private RecordServiceImpl recordService;

	private final String msgKey = "msg";
	private final String preKey = "pre";
	private final String resKey = "result";

	@RequestMapping("/login")
	@LoginValidate(needLogin = false)
	public String login(String stuNo, String stuPass, Model model) {
		String result = "/client/stu/login";
		try {
			if (stuNo != null && stuPass != null && !stuNo.equals("") && !stuPass.equals("")) {
				Student stu = studentService.login(stuNo, stuPass);
				if (stu != null) {
					// 缓存当天预约情况信息
					if (CacheUtil.getStuRecInfos().get(stu.getStuNo()) == null) {
						StuRecInfo info = new StuRecInfo();
						info.setIsHave(false);
						info.setCancel(recordService.queryCancel(stu.getStuId()));
						CacheUtil.getStuRecInfos().put(stu.getStuNo(), info);
						System.out.println("today cancel num is " + info.getCancel());
					}

					model.addAttribute(StringUtil.STUDENT_KEY, stu);
					result = "redirect:/client/stu";
				} else {
					model.addAttribute(msgKey, "账号或者密码不正确！");
				}
			}
		} catch (Exception e) {
			result = "/error";
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/logout")
	public String logout(@ModelAttribute(StringUtil.STUDENT_KEY) Student stu) {
		String result = "redirect:/client/stu/login";
		return result;
	}

	@RequestMapping("")
	public String student(String res) {
		String result = "/client/stu/" + res;
		if (res == null || res.equals("")) {
			result = "/client/stu/student";
		}
		return result;
	}

	@RequestMapping("/info")
	public String info(@ModelAttribute(StringUtil.STUDENT_KEY) Student stu, Model model) {
		String result = "/client/stu/info";
		try {
			Prestige pre = prestigeService.queryByStuNo(stu.getStuNo());
			if (pre == null) {
				pre = new Prestige();
				pre.setStuId(stu.getStuId());
				pre.setStuName(stu.getStuName());
				pre.setStuNo(stu.getStuNo());
				prestigeService.save(pre);
			}
			model.addAttribute(preKey, pre);
		} catch (Exception e) {
			result = "/error";
			e.printStackTrace();
		}
		return result;
	}
//修改学生密码信息
	@RequestMapping("/change")//
	@ResponseBody
	public String change(@ModelAttribute(StringUtil.STUDENT_KEY) Student stu, String oldPass, String newPass) {
		String result = "{\"" + resKey + "\":\"success\"}";
		try {
			Student student = studentService.queryByIdOrNo(stu.getStuId(), stu.getStuNo());
			if (student.getStuPass().equals(oldPass)) {
				student.setStuPass(newPass);
				if (studentService.update(student) == false) {
					result = "{\"" + resKey + "\":\"fail\"}";
				}
				student = null;
			}
		} catch (Exception e) {
			result = "{\"" + resKey + "\":\"fail\"}";
			e.printStackTrace();
		}
		return result;
	}
}
